package name.marz.to.go.ahg.common.util;

import java.net.URI;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import name.marz.to.go.ahg.core.Parameters;

import org.apache.http.Header;
import org.apache.http.cookie.Cookie;
import org.jsoup.nodes.Attribute;
import org.jsoup.nodes.FormElement;


public class LogUtil {
	private static final Logger log = Logger.getLogger(LogUtil.class.getName());
	
	public static void log(Level level, Header[] headers, String description) {
		log.log(level,description);
		if ( headers.length == 0 ) {
			log.log(level,"No headers");
		} else {
			for (int i = 0; i < headers.length; i++ ) {
				log.log(level,"header["+i+"] "+headers[i].getName()+" = "+headers[i].getValue());
			}
		}
	}
	public static void logForms(Level level, List<FormElement> forms ) {
		log.log(level,"Total Forms : "+forms.size());
		int i=0;
		for (FormElement form : forms) {
			for (Attribute a  : form.attributes()) {
				log.log(level,"form["+i+"] "+a.getKey()+"="+a.getValue());
				
			}
			Parameters p = HttpUtil.getParametersFromFormTag(form);
			log(level,p,"form["+i+"] params");
			i++;
		}
	}

	public static void log(Level level, List<Cookie> cookies, String description) {
		log.log(level,description);
		log(level,cookies);
	}
	public static void log(Level level, List<Cookie> cookies) {
		if ( cookies.isEmpty() ) {
			log.log(level,"No cookies");
		} else {
			int i = 0;
			for (Cookie cookie : cookies) {
				log.log(level,"cookie["+i+"]="+cookie.toString());
				i++;
			}
		}
	}
	public static void log(Level level, Parameters params, String description) {
		log.log(level,description);
		if ( params.isEmpty() ) {
			log.log(level,"No params");
		} else {
			int i = 0;
			for ( Map.Entry<String, String> p : params.entrySet()) {
				log.log(level,"param["+i+"]="+p.toString());
				i++;
			}
		}
		
	}
	public static void log(Level level, URI uri, String description) {
		log.log(level,description);
		log.log(level,"uri="+uri);
		log.log(level,"uri.authority="+uri.getAuthority());
		log.log(level,"uri.fragment="+uri.getFragment());
		log.log(level,"uri.host="+uri.getHost());
		log.log(level,"uri.path="+uri.getPath());
		log.log(level,"uri.port="+uri.getPort());
		log.log(level,"uri.query="+uri.getQuery());
		log.log(level,"uri.scheme="+uri.getScheme());
		log.log(level,"uri.schemespcificpart="+uri.getSchemeSpecificPart());
		log.log(level,"uri.userinfo="+uri.getUserInfo());
		log.log(level,"uri.rawpath="+uri.getRawPath());
		
	}
}
